package com.yanqu.road.server.cmd.group;

import com.yanqu.road.entity.decoration.MammonRecord;
import com.yanqu.road.entity.enums.eServerGroup2Type;
import com.yanqu.road.logic.pb.DecorationPb;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.pb.cross.group.CrossGroupProto;
import com.yanqu.road.pb.decoration.DecorationProto;
import com.yanqu.road.server.cmd.CrossCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.rank.CrossRankMgr;
import com.yanqu.road.server.manger.MammonConfigMgr;
import com.yanqu.road.server.manger.ServerGroup2Mgr;
import com.yanqu.road.server.manger.ServerGroupMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.Protocol;

import java.util.ArrayList;
import java.util.List;

@Cmd(code = Protocol.C_CROSS_GROUP_MAMMON_RECORD_CHANGE, desc = "跨服分组财神簿变化")
public class CrossGroupMammonRecordChangeCmd extends CrossCommand {

    private int decorationId;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {
        CrossGroupProto.CrossGroupMammonRecordChangeReqMsg reqMsg = CrossGroupProto.CrossGroupMammonRecordChangeReqMsg.parseFrom(packet.getBytes());
        decorationId = reqMsg.getDecorationId();

        // 跨地区称号要放到别的地方
        boolean isCrossArea = MammonConfigMgr.isCrossAreaMammonDecorationId(decorationId);
        int crossGroupId = ServerGroupMgr.getGroupIdByServerId(serverId);
        int crossAreaGroupId = ServerGroup2Mgr.getServerGroup2Id(eServerGroup2Type.CrossAreaMammon, serverId);

        // 处理数据
        List<String> dataList = new ArrayList<>();
        for(DecorationProto.MammonRecordTempMsg temMsg : reqMsg.getDataListList()) {
            MammonRecord mammonRecord = DecorationPb.parseMammonRecord(decorationId, temMsg);
            dataList.add(mammonRecord.getRecordUid());

            if (isCrossArea) {
                CrossRankMgr.changeAreaMammonRecord(crossAreaGroupId, mammonRecord);
            } else {
                CrossRankMgr.changeMammonRecord(crossGroupId, mammonRecord);
            }
        }

        CrossGroupProto.CrossGroupMammonRecordChangeRespMsg.Builder respMsg = CrossGroupProto.CrossGroupMammonRecordChangeRespMsg.newBuilder();
        respMsg.setDecorationId(decorationId);
        respMsg.addAllDataList(dataList);
        YanQuMessage pbMsg = YanQuMessageUtils.buildMessage(Protocol.S_CROSS_GROUP_MAMMON_RECORD_CHANGE, respMsg);
        MessageHelper.sendPacket(serverId, 0, pbMsg);
    }

    @Override
    public void sendData() {

    }
}
